/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sorm;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author sapan
 */
public class Query {
    private String query;
    public Query(String query){
        this.query = query;
    }
    public HashMap[] runQuery(){
        ArrayList results = new ArrayList();
        MySqlConnection conn = ConnectionPool.leaseConnection();
        try {
            ResultSet rs = conn.executeQuery(this.query);
            ResultSetMetaData rsm = rs.getMetaData();
            while (rs.next()) {
                HashMap temp = new HashMap();
                for (int i = 1; i <= rsm.getColumnCount(); i++) {
                    String columnName = rsm.getColumnName(i).toLowerCase();
                    temp.put(columnName.toLowerCase(), rs.getString(columnName));
                }
                results.add(temp);
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            ConnectionPool.releaseConnection(conn);
        }
        return ((HashMap[])results.toArray(new HashMap[0]));
    }
    
    public static void main(String [] args){
        HashMap[] maps = new Query("select encounterId, starttime,patientId from enc where encounterid > 82093").runQuery();
         for (int i=0;i<maps.length;i++){
            HashMap map= maps[i];
            
            System.out.println(map);
        }
    }
}
